//
//  CustomerListVC.m
//  New_Runway
//
//  Created by mack on 2018/2/8.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CustomerListVC.h"
#import "FollowCarCustomerModel.h"
#import "CusmoterListCell.h"
#import "CustomerDetailVC.h"
@interface CustomerListVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *followCarArray;
@property (nonatomic, strong) NSMutableArray *followSeriesArray;
@property (nonatomic, strong) NSMutableArray *followPriceArray;

@end

@implementation CustomerListVC
-(NSMutableArray *)followCarArray{
    
    if (!_followCarArray) {
        _followCarArray = [[NSMutableArray alloc] init];
    }
    return _followCarArray;
}

-(NSMutableArray *)followSeriesArray{
    if (!_followSeriesArray) {
        _followSeriesArray = [[NSMutableArray alloc] init];
    }
    return _followSeriesArray;
    
}
-(NSMutableArray *)followPriceArray{
    
    if (!_followPriceArray) {
        _followPriceArray = [[NSMutableArray alloc] init];
    }
    return _followPriceArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"销售商机";
    self.view.backgroundColor = kColorWhite();
    
    [self initTableview];
    [self getfollowCar];
    [self getfollowSeries];
    [self getFollowCarPrice];
}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = MAIN_COLOR();
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerNib:[UINib nibWithNibName:@"CusmoterListCell" bundle:nil] forCellReuseIdentifier:@"CusmoterListCell"];
//    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_offset(self.navH);
        make.bottom.equalTo(weakSelf.view).mas_offset(-self.BotH);
        make.left.right.equalTo(weakSelf.view);
    }];
    
    
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return self.followCarArray.count;
    }else if (section == 1){
        return self.followSeriesArray.count;
    }else if (section == 2){
        return self.followPriceArray.count;
    }
    return 5;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CusmoterListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CusmoterListCell"];
    cell.index = indexPath;
    if (indexPath.section == 0) {
        CustomerResult *result = self.followCarArray[indexPath.row];
        
        cell.customerModel = result;
        
    }else if (indexPath.section == 1){
        CustomerResult *result = self.followSeriesArray[indexPath.row];
        cell.customerModel = result;

    }else if (indexPath.section == 2){
       CustomerResult *result =  self.followPriceArray[indexPath.row];
        cell.customerModel = result;

    }
    
    
    return cell;
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.followCarArray.count == 0) {
        if (section == 0) {
            return nil;
        }
    }
    if (self.followSeriesArray.count == 0) {
        if (section == 1) {
            return nil;
        }
    }
    if (self.followPriceArray.count == 0) {
        if (section == 2) {
            return nil;
        }
    }

    NSArray *array = @[@"关注该车辆用户",@"关注同车系用户推荐",@"关注同价位用户推荐"];
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColorHex(0xf5f5f5);
    UILabel *titleL = [[UILabel alloc] init];
    titleL.text = array[section];
    titleL.textColor = UIColorHex(0x323232);
    titleL.font = [UIFont systemFontOfSize:14];
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view).mas_offset(16);
        make.centerY.equalTo(view);
    }];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.followCarArray.count == 0) {
        if (section == 0) {
            return 0.0001;
        }
    }
    if (self.followSeriesArray.count == 0) {
        if (section == 1) {
            return 0.0001;
        }
    }
    if (self.followPriceArray.count == 0) {
        if (section == 2) {
            return 0.0001;
        }
    }
    return 30;
}
- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    NSIndexPath *index = userInfo[@"index"];
    CustomerResult *result;
    if (index.section == 0) {
        result  = self.followCarArray[index.row];
    }else if (index.section == 1){
        result = self.followSeriesArray[index.row];
    }else if (index.section == 2){
        result = self.followPriceArray[index.row];

    }
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",result.customerMobile];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
  
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CustomerResult *result;
    if (indexPath.section == 0) {
        result  = self.followCarArray[indexPath.row];
    }else if (indexPath.section == 1){
        result = self.followSeriesArray[indexPath.row];
    }else if (indexPath.section == 2){
        result = self.followPriceArray[indexPath.row];
        
    }
    CustomerDetailVC *detailVC = [[CustomerDetailVC alloc] init];
    detailVC.customerId = result.customerShopId;
    [self.navigationController pushViewController:detailVC animated:YES];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//车id查询
-(void)getfollowCar{
    
    NSString *url = [NSString stringWithFormat:@"%@%@",CRMContextApi,followCarApi];
    NSDictionary *params = @{
                             @"marketId":self.carInfo.marketId,
                             @"shopId":self.carInfo.shopId ? self.carInfo.shopId : @"",
                             @"carId":self.carInfo.carId
                             };
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        
       FollowCarCustomerModel *customerModel = [FollowCarCustomerModel mj_objectWithKeyValues:response];
        if (customerModel.code == 1) {
            
            for (NSDictionary *dic in customerModel.result) {
                CustomerResult *result = [CustomerResult mj_objectWithKeyValues:dic];
                [self.followCarArray addObject:result];
            }
            
            [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];

        }
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
    
    
}
//车系ID查询
-(void)getfollowSeries{
    NSString *carSeries;
    if (self.carInfo.seriesId) {
        carSeries = [NSString stringWithFormat:@"%zd",self.carInfo.seriesId];
    }else{
        carSeries = @"";
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",CRMContextApi,followCarApi];
    NSDictionary *params = @{
                            @"carId":self.carInfo.carId,
                             @"marketId":self.carInfo.marketId,
                             @"shopId":self.carInfo.shopId ? self.carInfo.shopId : @"",
                             @"seriesId":carSeries
                             };
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        FollowCarCustomerModel *customerModel = [FollowCarCustomerModel mj_objectWithKeyValues:response];
        if (customerModel.code == 1) {
            
            for (NSDictionary *dic in customerModel.result) {
                CustomerResult *result = [CustomerResult mj_objectWithKeyValues:dic];
                [self.followSeriesArray addObject:result];
            }
            [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];

            
        }
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
    
    
}
-(void)getFollowCarPrice{
    NSString *url = [NSString stringWithFormat:@"%@%@",CRMContextApi,followCarPriceApi];
    NSDictionary *params = @{
                             @"marketId":self.carInfo.marketId,
                             @"shopId":self.carInfo.shopId ? self.carInfo.shopId : @"",
                             @"carId":self.carInfo.carId,
                             @"carStockState": @(self.carInfo.carStockState)
                             };
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        FollowCarCustomerModel *customerModel = [FollowCarCustomerModel mj_objectWithKeyValues:response];
        if (customerModel.code == 1) {
            
            for (NSDictionary *dic in customerModel.result) {
                CustomerResult *result = [CustomerResult mj_objectWithKeyValues:dic];
                [self.followPriceArray addObject:result];
            }
            [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
            
            
        }
        if (self.followSeriesArray.count ==0 && self.followPriceArray.count == 0 && self.followCarArray.count == 0) {
            [self.tableview addDefaultViewWithText:@"暂无数据哦！" defaultImageType:DefaultImageTypeNoData];
        }else{
            [self.tableview removeDefaultView];
        }
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];

    
}

@end
